package view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JLabel;

import model.Call;
import model.EnumCallStatus;
import model.Model;
import view.items.PanelActiveCallEstablished;
import view.items.PanelActiveCallRequestSend;
import view.items.PanelEndCall;
import view.items.PanelTitre;
import controller.Controller;
import controller.ControllerLighter;

public class ViewActiveCall extends ViewAbstract {

	
	private Model model;
	private Controller controller;
	private ControllerLighter controllerLighter;
	
	public ViewActiveCall(Controller controller,ControllerLighter controllerLighter, int x, int y, int width, int height){
		this.controller=controller;
		this.controllerLighter=controllerLighter;
		this.model = controller.getModel();
		this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 1));
		this.setBackground(Color.lightGray);
		this.setBounds(x,y,width,height);
		this.refresh();
	}
	
	
	
	public void refresh() {
		//System.out.println("refreshActivecall");
		this.removeAll();
		
		//Partie statique
		PanelTitre panelTitre = new PanelTitre("Active Call");
		this.add(panelTitre);

		//Partie dynamique
				
		List<Call> activeEsta = this.model.getCall(EnumCallStatus.ACTIV_ESTABLISHED);
		List<Call> activeReqSend = this.model.getCall(EnumCallStatus.ACTIV_REQUEST_SEND);
		List<Call> ended = this.model.getCall(EnumCallStatus.ENDED);
		List<Call> allActive = activeReqSend;
		allActive.addAll(activeEsta);
		allActive.addAll(ended);
		
		int nbCommActive = allActive.size();
		
		if(nbCommActive != 0){
			for (Call call : this.model.getAllCall()) {
				if(call.getStatus() == EnumCallStatus.ACTIV_REQUEST_SEND){
					PanelActiveCallRequestSend panelCallRequestSend = new PanelActiveCallRequestSend(call,controller,controllerLighter);
					this.add(panelCallRequestSend);
				}
				if(call.getStatus()==EnumCallStatus.ACTIV_ESTABLISHED){
					PanelActiveCallEstablished panelActiveCallEstablished= new PanelActiveCallEstablished(call,controller, controllerLighter);
					this.add(panelActiveCallEstablished);
				}
				if(call.getStatus()==EnumCallStatus.ENDED){
					PanelEndCall panelEndcall= new PanelEndCall(call,controller, controllerLighter);
					this.add(panelEndcall);
				}
				}
			}
		else{
			JLabel label;
			label = new JLabel("No active call");
			this.add(label);
		}
		this.repaint();
		this.revalidate();
	}
	
}
